package com.gyj.zookeeper.ZookeeperApi;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;

/**
 * Created by Gao on 2018/10/23.
 * create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
 * path:Znode路径，例如，/ myapp1，/ myapp2，/ myapp1 / mydata1，myapp2 / mydata1 / myanothersubdata
 * data:要存储在指定znode路径中的数据
 * acl:要创建的节点的访问控制列表.ZooKeeper API提供了一个静态接口ZooDefs.Ids来获取一些基本的acl列表。
 * 例如，ZooDefs.Ids.OPEN_ACL_UNSAFE返回打开znode的acl列表。
 * createMode - 节点的类型，即临时，顺序或两者。这是一个枚举。
 */
public class ZKCreate {

    private static ZooKeeper zk;

    private static ZKConnection connection;

    public static void create(String path, byte[] data) throws KeeperException, InterruptedException {
        zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    public static void main(String[] args) {
        String path = "/MyFirstZnode";

        // data in byte array
        byte[] data = "My First zookeeper app".getBytes();
        try {
            connection = new ZKConnection();
            zk = connection.connect("localhost");
            create(path, data);
            connection.close();
        } catch (IOException | InterruptedException | KeeperException e) {
            e.printStackTrace();
        }
    }
}
